package threadUtils.demo08;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 校验唤醒顺序是从一个睡眠的线程开始
 */
public class ConditionExample02 {


    public static void main(String[] args) throws InterruptedException {
        final ReentrantLock lock = new ReentrantLock();
        final Condition cLock = lock.newCondition();

        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                lock.lock();
                try {
                    System.out.println(Thread.currentThread().getName() + " enter");
                    cLock.await();
                    System.out.println(Thread.currentThread().getName() + " weak");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }

            }, "T-" + i).start();
        }

        Thread.sleep(1000);
        lock.lock();
        try {
            cLock.signal();
        } finally {
            lock.unlock();
        }
    }

}
